/**
 * 登录页
 * 
 * 设计：宋福勇
 * 
 * 审核优化：周俊伟
 * 
 * 最后修改日期:2020-09-17
 */
const db = wx.cloud.database();
const _ = db.command
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    icon: ''
  },
  //自动登录
  fastlogin(){
    wx.showLoading({
      title: '获取用户信息中',
    })
    wx.cloud.callFunction({
      name: 'login',
      data: {
        fastlogin: true,
      },
      complete: res => {
        wx.hideLoading();
        if(res.result.error == '0'){
          wx.showToast({
            icon:'none',
            title: '登录成功',
          })
          app.globalData.user_id = res.result.user_id;
          app.globalData.address1 = res.result.address1;
          app.globalData.address2 = res.result.address2;
          app.globalData.shop = res.result.shop;
          app.globalData.guest = res.result.guest;
          wx.redirectTo({
            url: '/pages/index/index',
          });
        }else{
          wx.showToast({
            icon:'none',
            title: '请先登录或体验',
          })
        }
      }
    })
  },
  // 登陆点击
  sign(e) {
    let user = e.detail.value.user.replace(/\s+/g, '');
    let password = e.detail.value.password.replace(/\s+/g, '');
    if (user === "") {
      wx.showModal({
        title: '提示',
        content: '请填写用户名',
        showCancel: false
      })
    } else if (password === "") {
      wx.showModal({
        title: '提示',
        content: '请填写密码',
        showCancel: false
      })
    } else {
      wx.cloud.callFunction({
        name: 'login',
        data: {
          user: user,
          password:password,
          guest:0,
        },
        complete: res => {
          console.log(res);
          if(res.result.error == '0'){
            app.globalData.user_id = res.result.user_id;
            app.globalData.address1 = res.result.address1;
            app.globalData.address2 = res.result.address2;
            app.globalData.shop = res.result.shop;
            app.globalData.guest = 0;
            wx.redirectTo({
              url: '/pages/index/index',
            });
          }else{
            wx.showModal({
              content: res.result.msg,
              showCancel:false,
            })
          }
        }
      })
    }
  },
  guest_login(){
    let user = 'xxxxx';
    let password = 'xxxx';
    wx.cloud.callFunction({
      name: 'login',
      data: {
        user: user,
        password:password,
        guest:1,
      },
      complete: res => {
        console.log(res);
        if(res.result.error == '0'){
          app.globalData.user_id = res.result.user_id;
          app.globalData.address1 = res.result.address1;
          app.globalData.address2 = res.result.address2;
          app.globalData.shop = res.result.shop;
          app.globalData.guest = 1;
          wx.redirectTo({
            url: '/pages/index/index',
          });
        }else{
          wx.showModal({
            content: res.result.msg,
            showCancel:false,
          })
        }
      }
    })
  },
  register() {
    wx.navigateTo({
      url: '../register/register',
    });

  },
  onLoad(){
    this.fastlogin();
  }
})